package com.example.demo1.service;


import com.example.demo1.entity.Comment;
import com.example.demo1.repository.CommentRepository;
import com.sun.el.stream.Optional;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class CommentService {

    @Autowired
    CommentRepository commentRepository;


//    public void SelectCommentById(Integer id){
//        Optional<Comment> comment = commentRepository.findById(id);
//        if(comment.isPresent())
//            System.out.println(comment.get());
//        else
//            System.out.println("not found");
//    }

    public List<Comment> SelectAllSortById(){
        return commentRepository.findAll(Sort.by(Sort.Direction.ASC,"aId"));
    }

    public List<Comment> SelectAllPage(int page,int size){
        Pageable pageable = PageRequest.of(page,size);
        return commentRepository.findAll(pageable).getContent();
    }

    public void addComment(String content,String author,Integer aId){
        Comment comment = new Comment();
        comment.setaId(aId);
        comment.setContent(content);
        comment.setAuthor(author);
        commentRepository.save(comment);

    }

    public List<Comment> SelectByExample(String author){
        Comment comment = new Comment();
        comment.setAuthor(author);
        //返回的是地址
        return commentRepository.findAll(Example.of(comment));
    }

    //分页
    public List<Comment> findAllPage(int page,int size){
        Pageable pageable = PageRequest.of(page,size);
        return commentRepository.findAll(pageable).getContent();
    }

}
